import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

export const asyncAdd = createAsyncThunk("counter/asyncAdd", () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("");
    }, 2000);
  });
});

const slice = createSlice({
  name: "counter",
  initialState: {
    num: 0,
  },
  reducers: {
    add(state) {
      state.num++;
    },
    minus(state) {
      state.num--;
    },
  },
  extraReducers: (builder) =>
    builder.addCase(asyncAdd.fulfilled, (state) => {
      state.num++;
    }),
});

export const { add, minus } = slice.actions;

export default slice.reducer;
